import { notification } from "antd"
import { auth_search, user_personal, user_register, user_delete, user_edit } from "../../utils/api"

// 创建一个同步action
export function create_roleaction_add(payload) {
    return { type: 'addrolelist', payload }
}
// 创建一个异步action
export function create_roleaddaction(payload) {
    return function (dispatch) {
        auth_search({ searchKey: '', page: 1, count: 100 }).then((res) => {
            dispatch(create_roleaction_add(res.data.list))
        })
    }
}
// 创建一个同步搜索action
export function create_roleaction_search(payload) {
    return { type: 'searchrolelist', payload }
}
// 创建一个异步搜索action
export function create_rolesearchaction(payload) {
    return function (dispatch) {
        console.log(payload);
        auth_search({ searchKey: payload, page: 1, count: 100 }).then((res) => {
            if (res.data.code == 200 && res.data.list.length != 0) {
                notification.success({
                    message: res.data.msg,
                    duration: 1
                })
                dispatch(create_roleaction_search(res.data.list))
            } else {
                notification.error({
                    message: "未查询到匹配的数据",
                    duration: 1
                });
                dispatch(create_roleaction_search(res.data.list))
            }

        })
    }
}
// 创建一个同步删除action
export function create_roleaction_delete(payload) {
    return { type: 'deleterolelist', payload }
}
// 创建一个异步删除action
export function create_roledeleteaction(payload) {
    return function (dispatch) {
        console.log(payload);
        user_delete(payload).then((res) => {
            console.log(payload);
            console.log(res.data);
            if (res.data.code == 200) {
                notification.success({
                    message: res.data.msg,
                    duration: 1
                })
                dispatch(create_roleaction_delete(res.data.list))
            } else {
                notification.error({
                    message: "未查询到匹配的数据",
                    duration: 1
                });
            }
        })
    }
}
// 创建一个同步新增数据到action
export function create_roleaction_newadd(payload) {
    return { type: 'newaddrolelist', payload }
}
// 创建一个异步新增数据到action
export function create_rolenewaddaction(payload) {
    return function (dispatch) {
        console.log(payload);
        user_register(payload).then((res) => {
            console.log(payload);
            console.log(res.data);
            if (res.data.code == 200) {
                console.log(payload);
                notification.success({
                    message: "新增成功",
                    duration: 1
                })
                auth_search({ searchKey: '', page: 1, count: 100 }).then((res) => {
                    dispatch(create_roleaction_newadd(res.data.list))
                });
            } else {
                notification.error({
                    message: "新增失败",
                    duration: 1
                });
            }
        })
    }
}


// 创建一个同步编辑数据到action
export function create_roleaction_edit(payload) {
    return { type: "editrolelist", payload }
}
// 创建一个异步编辑数据到action
export function create_roleeditaction(payload) {
    return function (dispatch) {
        console.log(payload);
        user_edit(payload).then((res) => {
            if (res.data.code == 200) {
                notification.success({ message: res.data.msg })
                dispatch(create_roleaddaction(res.data.list))
            } else {
                notification.error({ message: res.data.msg })
            }
        })
    }
}

// 创建保存用户信息的action
export function create_imgaction(payload) {
    console.log(payload);
    return { type: "adduserinfo", payload }
}
// 增加用户的头像到rudex
export function create_imgaction_add(payload) {
    console.log(payload);
    return { type: "adduserinfoimg", payload }
}
// 同步更新用户个人信息
export function create_userpersonal(payload) {
    console.log(payload);
    return { type: "addpersonalinfoimg", payload }
}
// 异步更新用户个人信息
export function create_userpersonal_add(payload) {
    return function (dispatch) {
        user_personal(payload).then((res) => {
            if (res.data.code == 200) {
                notification.success({
                    message: res.data.msg,
                    duration: 1
                })
                dispatch(create_userpersonal(res.data.userinfo))
            } else {
                notification.error({
                    message: res.data.msg,
                    duration: 1
                })
            }
        })
    }
}
